package test;

import communication.Client;
import model.InvertedPendulum;
import model.ModelParameter;
import util.ConversionUtil;
import util.DrawUtil;
import model.FirstOrderInvertedPendulumSystem;
import model.Massage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wujiazhan
 * @className test
 * @description 测试类
 * @date 2021-06-04 17:30
 */
@SuppressWarnings("all")
public class test {
    public static void main(String[] args) throws IOException {
        //多客户端测试
        Client client1 = new Client("1号");
        Client client2 = new Client("2号");
        Client client3 = new Client("3号");

        client1.start();
        //client2.start();
       // client3.start();
        //msgTest();
    }
    //系统测试
    public static void msgTest(){
        double samplingTime = 2.0 /1000;
        List<Massage> massages = new ArrayList<>();
        FirstOrderInvertedPendulumSystem system = new FirstOrderInvertedPendulumSystem(samplingTime);
        for (int i = 0; i < 5000; i++) {
            system.run(1.0);
            System.out.println("θ: " + system.getTheta() + " , x_car: " + system.getX_car());
            Massage massage = new Massage();
            massage.setTheta(system.getTheta());
            massage.setX_car(system.getX_car());
            massage.setX_tar(1.0);
            massages.add(massage);
        }
        DrawUtil.plot(massages, samplingTime);
    }
    //数据转换测试
    public static void doubleToByte(){
        double[] arr = new double[2];
        arr[0] = 1.0;
        arr[1] = 0.5;
        byte[] bytes = ConversionUtil.asByteArray(arr);
        System.out.println(Arrays.toString(bytes));
        System.out.println(Arrays.toString(ConversionUtil.asDoubleArray(bytes)));
    }


    //模型测试
    public static void modelTest(){
        ArrayList<Massage> list = new ArrayList<>();
        //摆角设置为pi/4
        InvertedPendulum invertedPendulum = new InvertedPendulum(new ModelParameter(1,1,0.6, Math.PI/4, 0.02,9.8));
        for (int i = 0; i < 5000; i++) {
            invertedPendulum.forwardAndRKIntegral(0);//输入系统的力设置为0
            if(list.size() < 4999){
                Massage massage = new Massage();
                massage.setX_car(invertedPendulum.getX_car());
                massage.setTheta(invertedPendulum.getTheta());
                System.out.println(massage);
                list.add(massage);
            }else {
                DrawUtil.plot(list,0.02);
            }
        }


    }

}
